import 'package:flutter/material.dart';
import 'package:sleep_manager/ui/widget/custom_app_bar.dart';
import 'package:sleep_manager/utils/navigation_utils.dart';
import 'package:sleep_manager/utils/toast_utils.dart';

/// 系统设置
class SystemSettingPage extends StatefulWidget {

  const SystemSettingPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _SystemSettingPageState();
  }

}

class _SystemSettingPageState extends State<SystemSettingPage> {
  // 提示音
  bool _openWarnSound = false;

  bool _shareData = false;

  bool _onlyTransferDataOnWifi = false;

  final NavigationUtils navigationUtils = NavigationUtils();

  /// 带switch的item
  Widget _buildSwitchItem(String title, bool status, void Function(bool) onChanged) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          height: 60,
          padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
          child: Row(
            mainAxisSize: MainAxisSize.max,
            children: [
              Text(
                title,
                style: const TextStyle(
                    fontSize: 12,
                    color: Colors.black,
                    fontWeight: FontWeight.bold),
              ),
              Expanded(child: Container()),
              SizedBox(
                width: 55,
                child: FittedBox(
                  fit: BoxFit.fitWidth,
                  child: Switch(
                      value: status,
                      activeColor: Colors.white, // 滑块颜色
                      activeTrackColor: const Color(0xFF0053F6), // 轨道颜色
                      inactiveThumbColor: Colors.white, // 关闭时滑块颜色
                      inactiveTrackColor: const Color(0xFFCCCCCC), // 关闭时轨道颜色
                      trackOutlineColor: WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
                        if (states.contains(WidgetState.selected)) {
                          return const Color(0xFF0053F6); // 当开关处于选中状态时轨道的颜色
                        }
                        return const Color(0xFFCCCCCC); // 默认轨道的颜色
                      }),
                      onChanged: (bool value) {
                        onChanged(value);
                      }),
                ),
              )
            ],
          ),
        ),
        Container(
          height: 1,
          color: const Color(0x4C707070),
        )
      ],
    );
  }

  /// 带箭头和数据的item
  Widget _buildArrowItem(String title, String cacheSize,void Function() onClick) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        InkWell(
          onTap: onClick,
          child: Container(
            height: 60,
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                      fontSize: 12,
                      color: Colors.black,
                      fontWeight: FontWeight.bold
                  ),
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      cacheSize,
                      style: const TextStyle(
                        color: Colors.black,
                        fontSize: 12,
                        fontWeight: FontWeight.bold
                      ),
                    ),
                    const Icon(Icons.arrow_forward_ios, size: 18)
                  ],
                )
              ],
            ),
          ),
        ),
        Container(
          height: 0.5,
          color: const Color(0x4C707070),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const CustomAppBar(
        title: '系统设置',
        showBackButton: true,
      ),
      body: Container(
        alignment: Alignment.topCenter,
        color: const Color(0xFFF7F7F7),
        child: Container(
          margin: const EdgeInsets.fromLTRB(16, 10, 16, 0),
          padding: const EdgeInsets.fromLTRB(16, 16, 16, 50),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                    color: Colors.black.withOpacity(0.1),
                    offset: const Offset(2, 2),
                    blurRadius: 4)
              ]),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              _buildSwitchItem('设备断开提示音', _openWarnSound, (bool value) {
                setState(() {
                  _openWarnSound = value;
                });
              }),
              _buildSwitchItem('共享数据', _shareData, (bool value) {
                setState(() {
                  _shareData = value;
                });
              }),
              _buildSwitchItem('仅在WIFI下传输数据', _onlyTransferDataOnWifi, (bool value) {
                setState(() {
                  _onlyTransferDataOnWifi = value;
                });
              }),
              _buildArrowItem('清除数据', "120M", () {
                showToast("清除数据");
              }),
              _buildArrowItem('修改密码', "",() {
                navigationUtils.navigateToPage(context, "");
              }),
              _buildArrowItem('注销账户', "",() {
                navigationUtils.navigateToPage(context, "");
              })
            ],
          ),
        ),
      ),
    );
  }
}
